home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / Debconf / DbDriver / LDAP.pm < prev    next >
Encoding:
Perl POD Document  |  2009-03-24  |  6.1 KB  |  264 lines

  1. #!/usr/bin/perl -w
  2. # This file was preprocessed, do not edit!
  3.  
  4.  
  5. package Debconf::DbDriver::LDAP;
  6. use strict;
  7. use Debconf::Log qw(:all);
  8. use Net::LDAP;
  9. use base 'Debconf::DbDriver::Cache';
  10.  
  11.  
  12. use fields qw(server port basedn binddn bindpasswd exists keybykey ds accept_attribute reject_attribute);
  13.  
  14.  
  15. sub binddb {
  16.     my $this=shift;
  17.  
  18.     $this->error("No server specified") unless exists $this->{server};
  19.     $this->error("No Base DN specified") unless exists $this->{basedn};
  20.     
  21.     $this->{binddn} = "" unless exists $this->{binddn};
  22.     $this->{port} = 389 unless exists $this->{port};
  23.     
  24.     debug "db $this->{name}" => "talking to $this->{server}, data under $this->{basedn}";
  25.  
  26.     $this->{ds} = Net::LDAP->new($this->{server}, port => $this->{port}, version => 3);
  27.     if (! $this->{ds}) {
  28.         $this->error("Unable to connect to LDAP server");
  29.         return; # if not fatal, give up anyway
  30.     }
  31.     
  32.     my $rv = "";
  33.     if (!($this->{binddn} && $this->{bindpasswd})) {
  34.         debug "db $this->{name}" => "binding anonymously; hope that's OK";
  35.         $rv = $this->{ds}->bind;
  36.     } else {
  37.         debug "db $this->{name}" => "binding as $this->{binddn}";
  38.         $rv = $this->{ds}->bind($this->{binddn}, password => $this->{bindpasswd});
  39.     }
  40.     if ($rv->code) {
  41.         $this->error("Bind Failed: ".$rv->error);
  42.     }
  43.     
  44.     return $this->{ds};
  45. }
  46.  
  47.  
  48. sub init {
  49.     my $this = shift;
  50.  
  51.     $this->SUPER::init(@_);
  52.  
  53.     $this->binddb;
  54.     return unless $this->{ds};
  55.  
  56.     $this->{exists} = {};
  57.     
  58.     if ($this->{keybykey}) {
  59.         debug "db $this->{name}" => "will get database data key by key";
  60.     }
  61.     else {
  62.         debug "db $this->{name}" => "getting database data";
  63.         my $data = $this->{ds}->search(base => $this->{basedn}, sizelimit => 0, timelimit => 0, filter => "(objectclass=debconfDbEntry)");
  64.         if ($data->code) {
  65.             $this->error("Search failed: ".$data->error);
  66.         }
  67.             
  68.         my $records = $data->as_struct();
  69.         debug "db $this->{name}" => "Read ".$data->count()." entries";    
  70.     
  71.         $this->parse_records($records);
  72.     
  73.         $this->{ds}->unbind;
  74.     }
  75. }
  76.  
  77.  
  78. sub shutdown
  79. {
  80.     my $this = shift;
  81.     
  82.     return if $this->{readonly};
  83.     
  84.     if (grep $this->{dirty}->{$_}, keys %{$this->{cache}}) {
  85.         debug "db $this->{name}" => "saving changes";
  86.     } else {
  87.         debug "db $this->{name}" => "no database changes, not saving";
  88.         return 1;
  89.     }
  90.     
  91.     unless ($this->{keybykey}) {
  92.         $this->binddb;
  93.         return unless $this->{ds};
  94.     }
  95.  
  96.     foreach my $item (keys %{$this->{cache}}) {
  97.         next unless defined $this->{cache}->{$item};  # skip deleted
  98.         next unless $this->{dirty}->{$item};    # skip unchanged
  99.         (my $entry_cn = $item) =~ s/([,+="<>#;])/\\$1/g;
  100.         my $entry_dn = "cn=$entry_cn,$this->{basedn}";
  101.         debug "db $this->{name}" => "writing out to $entry_dn";
  102.         
  103.         my %data = %{$this->{cache}->{$item}};
  104.         my %modify_data;
  105.         my $add_data = [ 'objectclass' => 'top',
  106.                 'objectclass' => 'debconfdbentry',
  107.                 'cn' => $item
  108.         ];
  109.  
  110.         my @fields = keys %{$data{fields}};
  111.         foreach my $field (@fields) {
  112.             my $ldapname = $field;
  113.             if ( $ldapname =~ s/_(\w)/uc($1)/ge ) {
  114.                 $data{fields}->{$ldapname} =  $data{fields}->{$field};
  115.                 delete $data{fields}->{$field};
  116.             }
  117.         }
  118.         
  119.         foreach my $field (keys %{$data{fields}}) {
  120.             next if ($data{fields}->{$field} eq '' && 
  121.                  !($field eq 'value'));
  122.             if ((exists $this->{accept_attribute} &&
  123.                  $field !~ /$this->{accept_attribute}/) or
  124.                 (exists $this->{reject_attribute} &&
  125.                  $field =~ /$this->{reject_attribute}/)) {
  126.                 debug "db $item" => "reject $field";
  127.                 next;
  128.             }
  129.  
  130.              $modify_data{$field}=$data{fields}->{$field};
  131.             push(@{$add_data}, $field);
  132.             push(@{$add_data}, $data{fields}->{$field});
  133.         }
  134.  
  135.         my @owners = keys %{$data{owners}};
  136.         debug "db $this->{name}" => "owners is ".join("  ", @owners);
  137.         $modify_data{owners} = \@owners;
  138.         push(@{$add_data}, 'owners');
  139.         push(@{$add_data}, \@owners);
  140.         
  141.         my @flags = grep { $data{flags}->{$_} eq 'true' } keys %{$data{flags}};
  142.         if (@flags) {
  143.             $modify_data{flags} = \@flags;
  144.             push(@{$add_data}, 'flags');
  145.             push(@{$add_data}, \@flags);
  146.         }
  147.  
  148.         $modify_data{variables} = [];
  149.         foreach my $var (keys %{$data{variables}}) {
  150.             my $variable = "$var=$data{variables}->{$var}";
  151.             push (@{$modify_data{variables}}, $variable);
  152.             push(@{$add_data}, 'variables');
  153.             push(@{$add_data}, $variable);
  154.         }
  155.         
  156.         my $rv="";
  157.         if ($this->{exists}->{$item}) {
  158.             $rv = $this->{ds}->modify($entry_dn, replace => \%modify_data);
  159.         } else {
  160.             $rv = $this->{ds}->add($entry_dn, attrs => $add_data);
  161.         }
  162.         if ($rv->code) {
  163.             $this->error("Modify failed: ".$rv->error);
  164.         }
  165.     }
  166.  
  167.     $this->{ds}->unbind();
  168.  
  169.     $this->SUPER::shutdown(@_);
  170. }
  171.  
  172.  
  173. sub load {
  174.     my $this = shift;
  175.     return unless $this->{keybykey};
  176.     my $entry_cn = shift;
  177.  
  178.     my $records = $this->get_key($entry_cn);
  179.     return unless $records;
  180.         
  181.     debug "db $this->{name}" => "Read entry for $entry_cn";
  182.  
  183.     $this->parse_records($records);
  184. }
  185.  
  186.  
  187. sub remove {
  188.     return 1;
  189. }
  190.  
  191.  
  192. sub save {
  193.     return 1;
  194. }
  195.  
  196.  
  197. sub get_key {
  198.     my $this = shift;
  199.     return unless $this->{keybykey};
  200.     my $entry_cn = shift;
  201.  
  202.     my $data = $this->{ds}->search(
  203.         base => 'cn=' . $entry_cn . ',' . $this->{basedn},
  204.         sizelimit => 0,
  205.         timelimit => 0,
  206.         filter => "(objectclass=debconfDbEntry)");
  207.  
  208.     if ($data->code) {
  209.         $this->error("Search failed: ".$data->error);
  210.     }
  211.  
  212.     return unless $data->entries;
  213.     $data->as_struct();
  214. }
  215.  
  216. sub parse_records {
  217.     my $this = shift;
  218.     my $records = shift;
  219.  
  220.     foreach my $dn (keys %{$records}) {
  221.         my $entry = $records->{$dn};
  222.         debug "db $this->{name}" => "Reading data from $dn";
  223.         my %ret = (owners => {},
  224.             fields => {},
  225.             variables => {},
  226.             flags => {},
  227.         );
  228.         my $name = "";
  229.  
  230.         foreach my $attr (keys %{$entry}) {
  231.             if ($attr eq 'objectclass') {
  232.                 next;
  233.             }
  234.             my $values = $entry->{$attr};
  235.  
  236.             $attr =~ s/([a-z])([A-Z])/$1.'_'.lc($2)/ge;
  237.  
  238.             debug "db $this->{name}" => "Setting data for $attr";
  239.             foreach my $val (@{$values}) {
  240.                 debug "db $this->{name}" => "$attr = $val";
  241.                 if ($attr eq 'owners') {
  242.                     $ret{owners}->{$val}=1;
  243.                 } elsif ($attr eq 'flags') {
  244.                     $ret{flags}->{$val}='true';
  245.                 } elsif ($attr eq 'cn') {
  246.                     $name = $val;
  247.                 } elsif ($attr eq 'variables') {
  248.                     my ($var, $value)=split(/\s*=\s*/, $val, 2);
  249.                     $ret{variables}->{$var}=$value;
  250.                 } else {
  251.                     $val=~s/\\n/\n/g;
  252.                     $ret{fields}->{$attr}=$val;
  253.                 }
  254.             }
  255.         }
  256.  
  257.         $this->{cache}->{$name} = \%ret;
  258.         $this->{exists}->{$name} = 1;
  259.     }
  260. }
  261.  
  262.  
  263. 1
  264.